package com.teyao.teyao_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.entity.User;
import com.teyao.teyao_back.mapper.UserMapper;
import com.teyao.teyao_back.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    //登录
    @Override
    public ResponseMessage login(User user) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername()).eq("password", user.getPassword());
        if (baseMapper.exists(queryWrapper)) {
            responseMessage.setSuccess(true);
        } else {
            responseMessage.setSuccess(false);
            responseMessage.setMsg("用户名或密码错误，登录失败");
        }
        if (baseMapper.exists(queryWrapper)) {
            responseMessage.setData(baseMapper.selectOne(queryWrapper));
        }
        return responseMessage;
    }

    //注册
    @Override
    public ResponseMessage register(User user) {
        ResponseMessage responseMessage = new ResponseMessage();
        boolean success;
        if (!user.getUsername().isBlank() && !user.getPassword().isBlank()) {
            user.setRoleId(11);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", user.getUsername());
            if (baseMapper.exists(queryWrapper)) {
                success = false;
                responseMessage.setMsg("用户名已存在");
            } else {
                success = baseMapper.insert(user) > 0 ? true : false;
            }
            responseMessage.setSuccess(success);
        } else {
            responseMessage.setSuccess(false);
            responseMessage.setMsg("用户名或密码为空");
        }
        return responseMessage;
    }


    //    詹
//查询 詹
    @Override
    public ResponseMessage updateList(Integer userId, User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        ResponseMessage responseMessage = new ResponseMessage();

        User userToUpdate = new User();
        userToUpdate.setUsername(user.getUsername());
        userToUpdate.setPassword(user.getPassword());
        userToUpdate.setPhone(user.getPhone());
        userToUpdate.setRoleId(user.getRoleId());
        int updateCount = baseMapper.update(userToUpdate, queryWrapper);
        boolean success = updateCount > 0 ? true : false;
        responseMessage.setSuccess(success);
        return responseMessage;
    }

    //删除 詹
    @Override
    public ResponseMessage deleteList(Integer userId) {
        // 构建删除条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        ResponseMessage responseMessage = new ResponseMessage();
        //执行删除操作
        int deleteCount = baseMapper.delete(queryWrapper);

        boolean success = deleteCount > 0 ? true : false;
        responseMessage.setSuccess(success);
        return responseMessage;
    }

}
